package com.language.points.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
/**
 * 代理示例 - 静态代理抽象类
 * @author yll
 * @version 1.0.0
 * @date 2024/04/25 14：40
 * */
public abstract class CglibProxy implements MethodInterceptor {
    private Object tarObj = null;
    private Object proxObj = null;
    public CglibProxy(Object tarObj){
        this.tarObj = tarObj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.tarObj.getClass());
        enhancer.setCallback(this);
        this.proxObj = enhancer.create();
    }
    public Object getProxObj(){
        return this.proxObj;
    }

    public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        invokeAfter(tarObj, proxy, method, objects, methodProxy);
        Object resultObj = methodProxy.invokeSuper(proxy, objects);
        invokeBefer(tarObj, proxy, method, objects, methodProxy);
        return resultObj;
    }

    public abstract void invokeAfter(Object tarObj, Object proxy, Method method, Object[] objects, MethodProxy methodProxy);
    public abstract void invokeBefer(Object tarObj, Object proxy, Method method, Object[] objects, MethodProxy methodProxy);

}
